/*
 *	lock.h:
 *	Spinlock with scoped destructors.
 *	Primarily used for task file register locking.
 */

#ifndef FTRACE_LOCK_H
#define FTRACE_LOCK_H

#include <pthread.h>


/**
 * Scoped rwlock.
 * Designed for ftrace task file register.
 * Provides RAII locking semantics for externally initilized pthread rwlock.
 */
class ScopeSharedLock {
public:

	/// Lock scope using external rwlock.
	ScopeSharedLock(pthread_rwlock_t* rwlock) : m_extlock(rwlock) {
		pthread_rwlock_rdlock(m_extlock);
	}

	~ScopeSharedLock() {
		pthread_rwlock_unlock(m_extlock);
	}

private:

	pthread_rwlock_t*	m_extlock;
};


class ScopeExclusiveLock {
public:

	/// Lock scope using external rwlock.
	ScopeExclusiveLock(pthread_rwlock_t* rwlock) : m_extlock(rwlock) {
		pthread_rwlock_wrlock(m_extlock);
	}

	~ScopeExclusiveLock() {
		pthread_rwlock_unlock(m_extlock);
	}

private:

	pthread_rwlock_t*	m_extlock;
};


#endif

